<template>
  <div class="scan-deatil mx5">
    <div>
      <el-tabs v-model="activeName" class="demo-tabs" style="display: inline">
        <el-form
          label-suffix="："
          :model="saveValue"
          :rules="rules"
          ref="formRef"
          label-width="150px"
        >
          <el-form-item label="数据标准" prop="markedTemplateId">
            <el-select
              v-model="saveValue.markedTemplateId"
              clearable
              @change="getModelList"
              filterable
            >
              <el-option
                v-for="item in markedTemplateInfo"
                :label="item.dataStandardName"
                :value="item.id"
                :key="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="是否分类分级" prop="marked">
            <el-radio-group v-model="saveValue.marked">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <div v-if="saveValue.marked == '1'">
            <el-form-item label="数据打标方式" prop="isDataMark">
              <el-row :gutter="24" style="width: 100%">
                <el-col :span="3" class="mb6">
                  <el-checkbox
                    style="display: inline"
                    label="rule"
                    true-label="1"
                    false-label="0"
                    class="pr5"
                    key="rule"
                    v-model="saveValue.ruleMark"
                    >规则打标</el-checkbox
                  >
                </el-col>
                <el-col :span="7">
                  <el-form-item
                    v-if="saveValue.ruleMark == '1'"
                    label="优先级"
                    label-width="110px"
                    :prop="saveValue.ruleMark == '1' ? 'rulePriority' : ''"
                  >
                    <ns-input
                      style="display: inline; width: 200px"
                      placeholder="请输入优先级"
                      v-model="saveValue.rulePriority"
                      clearable
                      type="number"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24" style="width: 100%">
                <el-col :span="3" class="mb6">
                  <el-checkbox
                    style="display: inline"
                    label="module"
                    class="pr5"
                    key="module"
                    true-label="1"
                    false-label="0"
                    v-model="saveValue.moduleMark"
                    >机器学习模型</el-checkbox
                  >
                </el-col>
                <el-col :span="7">
                  <el-form-item
                    v-if="saveValue.moduleMark == '1'"
                    label="优先级"
                    label-width="110px"
                    :prop="saveValue.moduleMark == '1' ? 'modulePriority' : ''"
                  >
                    <ns-input
                      style="display: block; width: 100%"
                      placeholder="请输入优先级"
                      v-model="saveValue.modulePriority"
                      type="number"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="10">
                  <el-form-item
                    label="机器学习模型"
                    :prop="saveValue.moduleMark == '1' ? 'moduleId' : ''"
                    label-width="140px"
                    v-if="saveValue.moduleMark == '1'"
                  >
                    <el-select
                      style="display: inline-block; width: 200px"
                      placeholder="请选择机器学习模型"
                      v-model="saveValue.moduleId"
                      clearable
                    >
                      <el-option
                        v-for="item in machineModel"
                        :label="item.name"
                        :value="item.id"
                        :key="item.id"
                      />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24" style="width: 100%">
                <el-col :span="3">
                  <el-checkbox
                    style="display: inline"
                    label="file"
                    key="file"
                    class="pr5"
                    true-label="1"
                    false-label="0"
                    v-model="saveValue.fileMark"
                    >文件相似度</el-checkbox
                  >
                </el-col>
                <el-col :span="7">
                  <el-form-item
                    v-if="saveValue.fileMark == '1'"
                    label="优先级"
                    label-width="110px"
                    :prop="saveValue.fileMark == '1' ? 'filePriority' : ''"
                  >
                    <ns-input
                      style="display: inline; width: 200px"
                      placeholder="请输入优先级"
                      v-model="saveValue.filePriority"
                      type="number"
                      clearable
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item
                    label="相似度指纹算法"
                    :prop="saveValue.fileMark == '1' ? 'likeSignAlgo' : ''"
                    label-width="140px"
                    v-if="saveValue.fileMark == '1'"
                  >
                    <el-checkbox-group v-model="saveValue.likeSignAlgo" style="display: inline">
                      <el-checkbox label="MD5" />
                      <el-checkbox label="KFS" />
                      <el-checkbox label="PFS" />
                      <el-checkbox label="PSA" />
                    </el-checkbox-group>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24" style="width: 100%">
                <el-col :span="3" />
                <el-col :span="21">
                  <el-form-item
                    label="相似度阈值"
                    label-width="110px"
                    :prop="saveValue.fileMark == '1' ? 'likeThreshold' : ''"
                    v-if="saveValue.fileMark == '1'"
                  >
                    <ns-input
                      style="display: inline; width: 123px"
                      v-model="saveValue.likeThreshold"
                      class="mr2"
                      clearable
                      @input="
                        saveValue.likeThreshold = saveValue.likeThreshold.replace(
                          /^0+(\d)|[^\d]+/g,
                          ''
                        )
                      "
                    />
                    百分比
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form-item>
          </div>
        </el-form>
      </el-tabs>
    </div>
    <div class="">
      <el-button type="primary" @click="save">确定</el-button>
      <el-button @click="closeOutDrawer">取消</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import * as scanStrategyApi from '@/api/asset/scanStrategy/index'
import { mapModelList } from '@/api/machineModel/classModel'
import { ElMessage } from 'element-plus'
import { dataStandardsPage } from '@/api/dataStandards/standards/index'
import { assetExecClassification } from '@/api/assetList'
const activeName = ref('first')
const formRef = ref()
const markedTemplateInfo = ref()
const message = useMessage() // 消息弹窗
const props = defineProps({
  params: {
    type: Object,
    default: () => {}
  },
  closeOutDrawer: Function
})

/**
 * @description:获取数据，并赋值扫描文件类型
 */
onMounted(() => {
  if (props.params) {
    scanStrategyApi.getScanPolicyTemplate(props.params.id).then((res) => {
      updateData(res)
    })
  }
  if (props.params.type === '1' || props.type === '1') {
    activeName.value = 'fourth'
  }
  //获取数据标准
  dataStandardsPage({ page: 1, pageSize: 10000, status: '1' }).then((res) => {
    markedTemplateInfo.value = res.data
  })
})
watch(
  () => props.params,
  (val) => {
    updateData(val)
  }
)
const updateData = (res) => {
  for (let i in res) {
    saveValue.value[i] = res[i]
  }
  if (saveValue.value.fileScanClass.length != 0) {
    saveValue.value.fileType = 2
  } else {
    saveValue.value.fileType = 1
  }
}

const validatePass = (rule: any, value: any, callback: any) => {
  if (
    (saveValue.value.moduleMark === '0' || saveValue.value.moduleMark === null) &&
    (saveValue.value.fileMark === '0' || saveValue.value.fileMark === null) &&
    (saveValue.value.ruleMark === '0' || saveValue.value.ruleMark === null)
  ) {
    callback(new Error('请选择数据打标方式'))
  } else {
    callback()
  }
}
const validateRule = (rule: any, value: any, callback: any) => {
  if (
    saveValue.value.fileMark == '1' &&
    saveValue.value.filePriority == saveValue.value.rulePriority &&
    saveValue.value.rulePriority != ''
  ) {
    callback(new Error('优先级不能相同'))
  } else if (
    saveValue.value.moduleMark == '1' &&
    saveValue.value.modulePriority == saveValue.value.rulePriority &&
    saveValue.value.rulePriority != ''
  ) {
    callback(new Error('优先级不能相同'))
  } else if (saveValue.value.ruleMark && saveValue.value.rulePriority == '') {
    callback(new Error('请输入优先级'))
  } else {
    callback()
  }
}
const validatemodule = (rule: any, value: any, callback: any) => {
  if (
    saveValue.value.fileMark == '1' &&
    saveValue.value.filePriority == saveValue.value.modulePriority &&
    saveValue.value.modulePriority != ''
  ) {
    callback(new Error('优先级不能相同'))
  } else if (
    saveValue.value.ruleMark == '1' &&
    saveValue.value.rulePriority == saveValue.value.modulePriority &&
    saveValue.value.modulePriority != ''
  ) {
    callback(new Error('优先级不能相同'))
  } else if (saveValue.value.moduleMark && saveValue.value.modulePriority == '') {
    callback(new Error('请输入优先级'))
  } else {
    callback()
  }
}
const validateFile = (rule: any, value: any, callback: any) => {
  if (
    saveValue.value.moduleMark == '1' &&
    saveValue.value.modulePriority == saveValue.value.filePriority &&
    saveValue.value.filePriority != ''
  ) {
    callback(new Error('优先级不能相同'))
  } else if (
    saveValue.value.ruleMark == '1' &&
    saveValue.value.rulePriority == saveValue.value.filePriority &&
    saveValue.value.filePriority != ''
  ) {
    callback(new Error('优先级不能相同'))
  } else if (saveValue.value.fileMark && saveValue.value.filePriority == '') {
    callback(new Error('请输入优先级'))
  } else {
    callback()
  }
}
const save = () => {
  formRef.value.validate(async (val) => {
    if (val) {
      saveValue.value.likeThreshold = Number(saveValue.value.likeThreshold)
      console.log('123', {
        scanPolicyTemplateSaveReqVO: saveValue.value,
        ids: props.params.ids,
        objectType: props.params.objectType
      })
      const data = await assetExecClassification({
        scanPolicyTemplateSaveReqVO: saveValue.value,
        ids: props.params.ids,
        objectType: props.params.objectType
      })
      console.log('data', data)
      if (data) {
        message.success('操作成功')
        props.params.callBack()
        props.closeOutDrawer()
      }
    }
  })
}
const saveValue = ref({
  scanCycleTime: 0,
  scanCycleNum: 1,
  markedTemplateId: '',
  scanCycle: 'month',
  templateName: '',
  scanCycleMethod: 1,
  templateDesc: '',
  templateType: '0',
  status: 0,
  marked: '1',
  scanCycleType: 1,
  dbScanType: 0,
  scanSamples: 1,
  scanSamplesLine: 10,
  scanSamplesType: 0,
  fileScanType: 0,
  fileSize: 0,
  likeThreshold: 0,
  likeSignAlgo: [],
  priority: '',
  moduleId: '',
  filePriority: '',
  modulePriority: '',
  rulePriority: '',
  fileScanClass: [],
  ruleMark: '0',
  moduleMark: '0',
  fileMark: '0',
  fileType: 1,
  isDataMark: ''
})
const rules = ref({
  scanCycle: [{ required: true, message: '请选择执行周期', trigger: 'change' }],
  scanCycleType: [{ required: true, message: '请选择扫描频率', trigger: 'change' }],
  scanCycleNum: [{ required: true, message: '请选择日期', trigger: 'change' }],
  scanCycleMethod: [{ required: true, message: '请选择执行方式', trigger: 'change' }],
  // dbScanType: [{ required: true, message: '请选择数据库采集方式', trigger: 'change' }],
  scanSamples: [{ required: true, message: '请选择是否扫描数据内容', trigger: 'change' }],
  scanSamplesLine: [{ required: true, message: '请选择抽样行数', trigger: 'change' }],
  scanSamplesType: [{ required: true, message: '请选择抽样方式', trigger: 'change' }],
  // fileScanType: [{ required: true, message: '请选择文件采集方式', trigger: 'change' }],
  fileSize: [{ required: true, message: '请输入文件过滤大小', trigger: 'blur' }],
  fileScanClass: [{ required: true, message: '请选择扫描文件类型', trigger: 'change' }],
  marked: [{ required: true, message: '请选择是否分类分级', trigger: 'change' }],
  likeSignAlgo: [{ required: true, message: '请选择相似度指纹算法', trigger: 'change' }],
  scanCycleTime: [{ required: true, message: '请选择时间', trigger: 'change' }],
  markedTemplateId: [{ required: true, message: '请选择数据标准', trigger: 'change' }],
  rulePriority: [{ validator: validateRule, required: true, trigger: 'blur' }],
  filePriority: [{ validator: validateFile, required: true, trigger: 'blur' }],
  modulePriority: [{ validator: validatemodule, required: true, trigger: 'blur' }],
  moduleId: [{ required: true, message: '请选择机器模型算法', trigger: 'change' }],
  fileType: [{ required: true, message: '请选择扫描文件类型', trigger: 'change' }],
  likeThreshold: [{ required: true, message: '请输入相似度阈值', trigger: 'blur' }],
  isDataMark: [{ validator: validatePass, trigger: 'change', required: true }]
})

const machineModel = ref([])
const getModelList = (id) => {
  mapModelList({ dataStandardId: id, page: 1, pageSize: 10000 }).then(({ data }) => {
    machineModel.value = data
  })
}
</script>

<style lang="scss">
.scan-deatil {
  .el-form-item {
    margin-bottom: 20px;
  }
}
</style>
